package com.link.base.base.product.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.common.model.Attachment;
import com.link.base.base.orgnization.model.Orgnization;
import com.link.base.base.product.model.PortalProductCase;
import com.link.base.base.product.service.PortalProductCaseService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 /**
  * 产品案例模块
  *
  * @author 路江
  * @date 2019/12/13 09:58
  */
 
@Controller
@RequestMapping("/link/portalProductCase")
public class ProdCaseController extends BasicController<PortalProductCase> {

    @Resource
    private PortalProductCaseService portalProductCaseService;

    @Override
    public BasicService<PortalProductCase> getBasicService() throws Exception {
        return portalProductCaseService;
    }

    /**
     * 上传产品案例图片
     *
     * @author 谭琛琨
     * @date 2019/12/13 09:59
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadImg(PortalProductCase entity, HttpSession session, HttpServletRequest request,
                                         HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> myfiles = multipartRequest.getFiles("uploadAttachment");
        try {
            // 插入附件表
            portalProductCaseService.uploadImg(entity, myfiles);
            PortalProductCase newEntry = portalProductCaseService.queryById(entity);
            result.put("success", true);
            result.put("newRow", newEntry);
            result.put("entity", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询案例图片
     *
     * @author crm4
     * @date 2019/12/13 10:02
     */
    @RequestMapping(value = "/queryCaseImgByCaseId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryCaseImgByCaseId(PortalProductCase productCase) {
        Map<String, Object> result = new HashMap<>();

        try {
            List<Attachment> list = portalProductCaseService.queryCaseImgByCaseId(productCase);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新案例主图片
     *
     * @author crm4
     * @date 2019/12/13 10:03
     */
    @RequestMapping(value = "/imageUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> imageUpdate(PortalProductCase productCase) {
        Map<String, Object> result = new HashMap<>();

        try {
            portalProductCaseService.imageUpdate(productCase);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除一条记录
     *
     * @author 路江
     * @date 2019/12/13 10:04
     */
    @Override
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Map<String, Object> deleteById(@JsonParam PortalProductCase entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            beforDelete(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-031");
            }
            portalProductCaseService.deleteById(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
    
    /**
     * 重写查询列表
     *
     * @author 路江
     * @date 2019/12/13 10:05
     */
    @Override
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps, @JsonParam PortalProductCase entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            //查询产品案例列表
            result = super.queryByExamplePage(qps, entity, session, request, response);
            // 获取点赞量、浏览量等等redis缓存数据
            List<PortalProductCase> list = (List<PortalProductCase>)result.get("rows");
            CoreUser user = UserUtil.getUser();
            for (PortalProductCase item : list) {
                portalProductCaseService.setStatisticsData(item, user.getId());
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }

        return result;
    }
    
    /**
     * 重写查询产品案例详情
     *
     * @author 路江
     * @date 2019/12/13 10:06
     */
    @Override
    public Map<String, Object> queryById(@JsonParam PortalProductCase entity, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            //获取产品案例详情
            PortalProductCase portalProductCase;
            portalProductCase = (PortalProductCase)super.queryById(entity, session, request, response).get("result");
            CoreUser user = UserUtil.getUser();
            //获取缓存参数，点赞收藏浏览等数据
            portalProductCaseService.setStatisticsData(portalProductCase, user.getId());
            //查询相关产品及正文图片
            if (StringUtils.equals(entity.getLoginType(),"GUIDE_APP")) {
                portalProductCase = portalProductCaseService.getportalProductCase(portalProductCase);
            }
            result.put("result",portalProductCase);
            result.put("success",true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除一条相关产品
     *
     * @author 路江
     * @date 2019/12/13 10:06
     */
    @RequestMapping(value = "/deleteByProductCase")
    @ResponseBody
    public Map<String, Object> deleteByProductCase(@JsonParam PortalProductCase entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            portalProductCaseService.deleteByProductCase(entity);
            result.put("success", true);
        }  catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
    
    /**
     * 新增一条相关产品
     *
     * @author 路江
     * @date 2019/12/13 10:07
     */
    @RequestMapping(value = "/insertProductCase")
    @ResponseBody
    public Map<String, Object> insertProductCase(@RequestBody PortalProductCase entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            portalProductCaseService.insertProductCase(entity);
            result.put("success", true);
        }  catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新一条相关产品
     *
     * @author 路江
     * @date 2019/12/13 10:07
     */
    @Override
    public Map<String, Object> upsert(@JsonParam PortalProductCase entity, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            result = super.upsert(entity, session, request, response);
            PortalProductCase portalProductCase = (PortalProductCase)result.get("newRow");
            result.put("newRow",portalProductCase);
            result.put("success",true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据产品id查询与之相关联的案例集合
     *
     * @author 单建平
     * @date 2019/12/13 10:08
     */
    @RequestMapping(value = "/queryByProductId")
    @ResponseBody
    public Map<String, Object> queryByProductId(@RequestBody PortalProductCase entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<PortalProductCase> list = portalProductCaseService.queryByProductId(entity);
            CoreUser user = UserUtil.getUser();
            for (PortalProductCase item: list) {
                //获取缓存参数，点赞收藏浏览等数据
                portalProductCaseService.setStatisticsData(item, user.getId());
            }
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据产品id查询与之相关联的案例集合（分享用）
     *
     * @author 谭琛琨
     * @date 2019/12/13 10:15
     */
    @RequestMapping(value = "/queryByCasebaseId")
    @ResponseBody
    public Map<String, Object> queryByCasebaseId(@JsonParam PortalProductCase entity, @JsonParam Orgnization org) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            PortalProductCase portalProductCase;
            portalProductCase = portalProductCaseService.queryByCasebaseId(entity);
            portalProductCase = portalProductCaseService.queryCasebaseImg(portalProductCase, org);
            result.put("success", true);
            result.put("result", portalProductCase);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
